<?php
namespace Admin\Controller;
use Think\Controller;
class InfrastructureController extends BaseController {
    
	protected function _initialize(){
		parent::_initialize();
	}
	
	//基础数据管理
	public function index(){
		//
	}
	

	//产品
	public function product(){
		$m=M('bb_product');
		if(I('param')){
			$type=I('param');
			$this->assign('param',I('param'));
		}
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$map['type']=$type;
		$data=$m->where($map)->page($p,$psize)->order('id asc')->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '产品管理','bb-search');
		foreach ($data as $k => $v){
				$data[$k]['pricenew']=$v['price']*0.01.'元';
		}
		if(I('param')){
			$this->assign('param',I('param'));
		}
		$this->assign('cache',$data);
		$this->display();
		
	}
	//产品设置
	public function product_set(){
		$m=M('bb_product');
		$id=I('id');
		if(I('param')){
			$this->assign('param',I('param'));
		}
		if(IS_POST){
			$p=I('post.');
			if($id){
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				$p['addtime']=time();
				$re=$m->add($p);
				if($re){
					$info['status']=1;
					$info['msg']='添加成功！';
				}else{
					$info['status']=0;
					$info['msg']='添加失败！';
				}
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$this->assign('cache',$data);
		} 
		$this->display();
	}
	
	//删除
	public function product_del(){
		$id=I('ids');//必须使用get方法
		$m=M('bb_product');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$re=$m->delete($id);
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
	
/////////////////////////////////////////////////////////////////////	
	//品牌精洗产品
	public function product_jx(){
		$m=M('bb_product_jx');
		if(I('param')){
			$type=I('param');
			$this->assign('param',I('param'));
		}
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$map['type']=$type;
		$data=$m->where($map)->page($p,$psize)->order('id asc')->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '产品管理','bb-search');
		foreach ($data as $k => $v){
				$data[$k]['pricenew']=$v['price']*0.01.'元';
		}
		if(I('param')){
			$this->assign('param',I('param'));
		}
		$this->assign('cache',$data);
		$this->display();
		
	}
	//产品设置
	public function product_jx_set(){
		$m=M('bb_product_jx');
		$id=I('id');
		if(I('param')){
			$this->assign('param',I('param'));
		}
		if(IS_POST){
			$p=I('post.');
			if($id){
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				$p['addtime']=time();
				$re=$m->add($p);
				if($re){
					$info['status']=1;
					$info['msg']='添加成功！';
				}else{
					$info['status']=0;
					$info['msg']='添加失败！';
				}
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$this->assign('cache',$data);
		} 
		$this->display();
	}
	
	//删除
	public function product_jx_del(){
		$id=I('ids');//必须使用get方法
		$m=M('bb_product_jx');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$re=$m->delete($id);
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
//===============================================
	//文章
	public function article(){
		$bread=array(
			'0'=>array(
				'name'=>'基础数据',
				'url'=>U('Admin/Infrastructure/index')
			),
			'1'=>array(
				'name'=>'文章',
				'url'=>U('Admin/Infrastructure/article')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		
		$m=M('bps_news');
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$map='';
		$data=$m->where($map)->page($p,$psize)->order('id asc')->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '产品管理','bb-search');
		$this->assign('cache',$data);
		$this->display();
	}
	//设置文章
	public function article_set(){
		$m=M('bps_news');
		$id=I('id');
		if(IS_POST){
			$p=I('post.');
			if($id){
				$p['edittime']=date("Y-m-d H:i:s",time());
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				$p['adduser']='admin';
				$p['addtime']=date("Y-m-d H:i:s",time());
				$re=$m->add($p);
				if($re){
					$info['status']=1;
					$info['msg']='添加成功！';
				}else{
					$info['status']=0;
					$info['msg']='添加失败！';
				}
			}
			$this->ajaxreturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$this->assign('cache',$data);
		} 
		$this->display();
	}
	
	//删除
	public function article_del(){
		$id=I('id');//必须使用get方法
		$m=M('bps_news');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$re=$m->delete($id);
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
	
	
	//===========================
	//CMS后台广告分组
	public function ads(){
		//设置面包导航，主加载器请配置
		$bread=array(
			'0'=>array(
				'name'=>'基础数据',
				'url'=>U('Admin/Infrastructure/index')
			),
			'1'=>array(
				'name'=>'广告',
				'url'=>U('Admin/Infrastructure/ads')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//绑定搜索条件与分页
		$m=M('ads');
		$p=$_GET['p']?$_GET['p']:1;
		$name=I('name')?I('name'):'';
		if($name){
			$map['name']=array('like',"%$name%");
			$this->assign('name',$name);
		}
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$cache=$m->where($map)->page($p,$psize)->select();
		foreach($cache as $k=>$v){
			$listpic=$this->getPic($v['pic']);
			$cache[$k]['imgurl']=$listpic['imgurl'];
		}
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'Joel-loader', '商城广告','Joel-search');
		$this->assign('cache',$cache);		
		$this->display();
	}
	
	//CMS后台广告设置
	public function adsSet(){
		$id=I('id');
		$m=M('ads');
		//设置面包导航，主加载器请配置		
		$bread=array(
			'0'=>array(
				'name'=>'基础数据',
				'url'=>U('Admin/Infrastructure/index')
			),
			'1'=>array(
				'name'=>'广告',
				'url'=>U('Admin/Infrastructure/ads')
			)
		);
		$this->assign('breadhtml',$this->getBread($bread));
		//处理POST提交	
		if(IS_POST){
			//die('aa');
			$data=I('post.');
			if($id){
				$re=$m->save($data);
				if(FALSE!==$re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}else{
				$re=$m->add($data);
				if($re){
					$info['status']=1;
					$info['msg']='设置成功！';
				}else{
					$info['status']=0;
					$info['msg']='设置失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		//处理编辑界面
		if($id){			
			$cache=$m->where('id='.$id)->find();
			$this->assign('cache',$cache);	
		}	
		$this->display();
	}
	
	public function adsDel(){
		$id=I('id');//必须使用get方法
		$m=M('ads');
		if(!$id){
			$info['status']=0;
			$info['msg']='ID不能为空!';
			$this->ajaxReturn($info);
		}
		$re=$m->delete($id);
		if($re){
			$info['status']=1;
			$info['msg']='删除成功!';
		}else{
			$info['status']=0;
			$info['msg']='删除失败!';
		}
		$this->ajaxReturn($info);
	}
	//========================
	
	
	
	//业务员列表
	public function agent(){
		$m=M('bb_agent');
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$map='';
		$data=$m->where($map)->page($p,$psize)->order('addtime desc')->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '业务员管理','bb-search');
		foreach ($data as $k => $v){
			
		}
		$this->assign('cache',$data);
		$this->display();
	}
	//修改
	public function agent_set(){
		$id=I('id');
		$m=M('bb_agent');
		if(IS_POST){
			$p=I('post.');
			$re=$m->save($p);
			if($re){
				$info['status']=1;
				$info['msg']='修改成功！';
			}else{
				$info['status']=0;
				$info['msg']='修改失败！';
			}
			$this->ajaxReturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$this->assign('cache',$data);
		}
		$this->display();
	}
	//删除业务员
	public function agent_del(){
		$id=I('ids');
		if($id){
			$id=rtrim($id, ',');
			$re=M('bb_agent')->delete($id);
			if($re){
				$info['status']=1;
				$info['msg']='删除成功!';
			}else{
				$info['status']=0;
				$info['msg']='删除失败!';
			}
			$this->ajaxreturn($info);
		}
	}
//================================================
	
	//瑕疵
	public function defect(){
		$m=M('bb_defect');
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$map='';
		$data=$m->where($map)->page($p,$psize)->order('id desc')->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '业务员管理','bb-search');
		$this->assign('cache',$data);
		$this->display();
	}
	//设置瑕疵
	public function defect_set(){
		$id=I('id');
		$m=M('bb_defect');
		if(IS_POST){
			$p=I('post.');
			if($id){
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				$re=$m->add($p);
				if($re){
					$info['status']=1;
					$info['msg']='添加成功！';
				}else{
					$info['status']=0;
					$info['msg']='添加失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$this->assign('cache',$data);
		}
		$this->display();
	}
	//删除瑕疵
	public function defect_del(){
		$id=I('ids');
		if($id){
			$id=rtrim($id, ',');
			$re=M('bb_defect')->delete($id);
			if($re){
				$info['status']=1;
				$info['msg']='删除成功!';
			}else{
				$info['status']=0;
				$info['msg']='删除失败!';
			}
			$this->ajaxreturn($info);
		}
	}
	
//======================================================
		//附件
	public function annex(){
		$m=M('bb_annex');
		$p=$_GET['p']?$_GET['p']:1;
		$psize=C('BB_PSIZE')?C('BB_PSIZE'):20;
		$map='';
		$data=$m->where($map)->page($p,$psize)->order('id desc')->select();
		$count=$m->where($map)->count();
		$this->getPage($count, $psize, 'bb-loader', '业务员管理','bb-search');
		$this->assign('cache',$data);
		$this->display();
	}
	//设置附件
	public function annex_set(){
		$id=I('id');
		$m=M('bb_annex');
		if(IS_POST){
			$p=I('post.');
			if($id){
				$re=$m->save($p);
				if($re){
					$info['status']=1;
					$info['msg']='修改成功！';
				}else{
					$info['status']=0;
					$info['msg']='修改失败！';
				}
			}else{
				$re=$m->add($p);
				if($re){
					$info['status']=1;
					$info['msg']='添加成功！';
				}else{
					$info['status']=0;
					$info['msg']='添加失败！';
				}
			}
			$this->ajaxReturn($info);
		}
		if($id){
			$data=$m->where(array('id'=>$id))->find();
			$this->assign('cache',$data);
		}
		$this->display();
	}
	//删除附件
	public function annex_del(){
		$id=I('ids');
		if($id){
			$id=rtrim($id, ',');
			$re=M('bb_annex')->delete($id);
			if($re){
				$info['status']=1;
				$info['msg']='删除成功!';
			}else{
				$info['status']=0;
				$info['msg']='删除失败!';
			}
			$this->ajaxreturn($info);
		}
	}
	//=====
}